import dictService from '@/services/DictionaryService.js'
import * as orgService from '@/services/OrganizationService'
import * as crewsService from '@/services/CrewsService.js'
import * as dockService from '@/services/DockService.js'
import * as portRegShipsService from '@/services/PortRegShipsService.js'
import * as berthsService from '@/services/BerthsService.js'
import * as transCompaniesService from '@/services/TransCompaniesService.js'
import DataTable from '@/components/common/DataTable.vue'
import moment from 'moment'

export default {
  components: {
    'data-table': DataTable
  },
  data: function () {
    return {
      activeName: '1',
      dockOptions: [],
      cjgOptions: [],
      hqOptions: [],
      cbOptions: [],
      jgOptions: [],
      jymsOptions: [],
      bwOptions: [],
      gxjgOptions: [],
      gsxzOptions: [],
      lbOptions: [],
      regPortName: '',
      sailingAreaName: '',
      typeNameCn: '',
      structure: '',
      operationMode: '',
      berthType: '',
      type: '',
      manageOrg: '',
      inSystem: '2',
      type2: '',
      totalPage: 1,
      totalPage1: 0,
      totalPage2: 0,
      totalPage3: 0,
      totalPage4: 0,
      totalPage5: 0,
      currentPage: 1,
      currentPage1: 1,
      currentPage2: 1,
      currentPage3: 1,
      currentPage4: 1,
      currentPage5: 1,
      organization: [],
      pid: ''
    }
  },
  created() {
    this.getOrg()
    this.getSelect()
  },
  methods: {
    getOrg() {
      orgService.findAll().then(async res => {
        await res.dataAll.sort((a, b) => {
          if (a.businessOrg < b.businessOrg) {
            return -1
          }
          if (a.businessOrg > b.businessOrg) {
            return 1
          }
          return 0
        })
        this.organization = await res.dataAll.filter(item => {
          return item.businessOrg == '3'
        })
      })
    },
    getSelect() {
      if (this.activeName == '1') {
        if (this.cjgOptions.length == 0) {
          dictService.find('CJG').then(res => {
            this.cjgOptions = res
          })
        }
        if (this.hqOptions.length == 0) {
          dictService.find('HQ').then(res => {

            this.hqOptions = res
          })
        }
        if (this.cbOptions.length == 0) {
          dictService.find('CBLX').then(res => {
            this.cbOptions = res
          })
        }
      } else if (this.activeName == '2') {
        if (this.jgOptions.length == 0) {
          dictService.find('BWJG').then(res => {

            this.jgOptions = res
          })
        }
        if (this.jymsOptions.length == 0) {
          dictService.find('BWJYMS').then(res => {

            this.jymsOptions = res
          })
        }
        if (this.bwOptions.length == 0) {
          dictService.find('BWXZ').then(res => {

            this.bwOptions = res
          })
        }
      } else if (this.activeName == '3') {
        if (this.gsxzOptions.length == 0) {
          dictService.find('GSXZ').then(res => {
            this.gsxzOptions = res
          })
        }
        if (this.gxjgOptions.length == 0) {
          dictService.find('GXJG').then(res => {
            this.gxjgOptions = res
          })
        }
      } else if (this.activeName == '4') {
        if (this.lbOptions.length == 0) {
          dictService.find('CYLB').then(res => {
            this.lbOptions = res
          })
        }
      }
      dockService.searchAll(0).then(res => {
        this.dockOptions = res.data
      })
    },
    handleClick(tab, event) {
      if (this.activeName == '1') {
        this.currentPage = this.currentPage1
        this.search()
      } else if (this.activeName == '2') {
        this.currentPage = this.currentPage2
        this.search()
      } else if (this.activeName == '3') {
        this.currentPage = this.currentPage3
        this.search()
      } else if (this.activeName == '4') {
        this.currentPage = this.currentPage4
        this.search()
      }
      this.getSelect()
    },
    search(val = 1) {
      if (this.activeName == '1') {
        this.$refs.portRegShip.search({
          regPortName: this.regPortName,
          sailingAreaName: this.sailingAreaName,
          typeNameCn: this.typeNameCn,
          currentPage: val
        })
      } else if (this.activeName == '2') {
        this.$refs.berth.search({
          structure: this.structure,
          operationMode: this.operationMode,
          berthType: this.berthType,
          currentPage: val
        })
      } else if (this.activeName == '3') {
        this.$refs.transCompany.search({
          type: this.type,
          manageOrg: this.manageOrg,
          inSystem: this.inSystem,
          currentPage: val
        })
      } else if (this.activeName == '4') {
        this.$refs.crew.search({type2: this.type2, currentPage: val})
      }
    }
  }
}
